﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.SYS.Supplier
{
    public partial class Form : FormPage
    {
        BLL.Supplier bll = new BLL.Supplier();
        Model.Supplier model = new Model.Supplier();
        //
        BLL.PAUser bllUser = new BLL.PAUser();
        Model.PAUser modelUser = new Model.PAUser();
        //
        BLL.Pic bllPic = new BLL.Pic();
        Model.Pic modelPic = new Model.Pic();
        protected void Page_Load(object sender, EventArgs e) { }
        /// <summary>
        /// 初始化本页Model
        /// </summary>
        public override void Page_Load_DefaultModel()
        {
            model = bll.GetModel(Master.id);
            modelUser = bllUser.GetModel(model.UserID);
            if (DataTypePlus.requestInt(model.PicID) > 0) modelPic = bllPic.GetModel(DataTypePlus.requestInt(model.PicID));
        }
        /// <summary>
        /// 初始化表单
        /// </summary>
        public override void Form_Default()
        {
            if (Security.GetCurrentUserType() != EnumHelper.UserType.Administrator && Security.GetCurrentUserType() != EnumHelper.UserType.SupplierServer)
            {
                group_IsLock.Visible = false;
                group_StateCheck.Visible = false;
            }
        }
        /// <summary>
        /// 绑定表单数据【修改时用】
        /// </summary>
        public override void Form_Bind()
        {
            tb_Password_helper.Visible = true;

            tb_Name.Text = model.Name;
            tb_ShortName.Text = model.ShortName;
            tb_Contacts.Text = model.Contacts;
            if (model.Tel != "")
            {
                tb_Tel_area.Text = model.Tel.Substring(0, 4);
                tb_Tel_real.Text = model.Tel.Substring(4);
            }
            tb_Mobile.Text = model.Mobile;
            tb_Address.Text = model.Address;
            tb_Description.Text = model.Description;

            tb_Province.Text = model.Province;
            tb_City.Text = model.City;
            tb_Area.Text = model.Area;

            tb_Pic.Text = modelPic.URL;

            cb_IsLatePay.Checked = model.IsLatePay;
            cb_Delivery.Checked = model.IsDelivery;

            cb_IsLock.Checked = modelUser.IsLock;

            if (Security.GetCurrentUserType() == EnumHelper.UserType.Administrator)
            {
                group_StateCheck.Visible = modelUser.State == (int)EnumHelper.UserState.WaitCheck;
                group_IsLock.Visible = modelUser.State == (int)EnumHelper.UserState.Nomal;
            }
        }
        /// <summary>
        /// 提交
        /// </summary>
        public override void Form_Submit()
        {
            string Name = PageValidate.SqlTextClear(DataTypePlus.requestString(tb_Name.Text));
            string Password = PageValidate.SqlTextClear(tb_Password.Text); ;
            string Mobile = PageValidate.SqlTextClear(tb_Mobile.Text);
            string Tel = PageValidate.SqlTextClear(tb_Tel_area.Text + tb_Tel_real.Text);

            if (
                Master.noErr(Name == "", "[供货商名称]必须填写") &&
                Master.noChongfu("Name='" + Name + "'", "[供货商名称]重复") &&
                Master.noChongfu("Mobile='" + Mobile + "'", "[手机号码]重复") &&
                Master.noChongfu("Tel<>'' and Tel='" + Tel + "'", "[办公电话]重复")
                )
            {
                //图片
                string Pic = DataTypePlus.requestString(tb_Pic.Text);
                if (model.PicID > 0 && modelPic.URL != Pic) bllPic.Delete(modelPic.id);//删除老图
                if (Pic == "") model.PicID = 0;
                else if (modelPic.URL != Pic)
                {
                    modelPic.URL = Pic;
                    System.Drawing.Image bmp = System.Drawing.Image.FromFile(this.Server.MapPath(Server.UrlDecode(Pic)));
                    modelPic.Width = bmp.Width;
                    modelPic.Height = bmp.Height;
                    model.PicID = bllPic.Add(modelPic);
                }
                //
                string ShortName = PageValidate.SqlTextClear(tb_ShortName.Text);
                string Contacts = PageValidate.SqlTextClear(tb_Contacts.Text);
                string Address = PageValidate.SqlTextClear(tb_Address.Text);
                string Description = tb_Description.Text;
                string Province = PageValidate.SqlTextClear(tb_Province.Text);
                string City = PageValidate.SqlTextClear(tb_City.Text);
                string CityNo = PageValidate.SqlTextClear(tb_CityNo.Text);
                string Area = PageValidate.SqlTextClear(tb_Area.Text);
                bool IsLatePay = cb_IsLatePay.Checked;
                bool IsDelivery = cb_Delivery.Checked;
                EnumHelper.UserState State = EnumHelper.UserState.WaitCheck;

                CallBak<int> callBak = new CallBak<int>();
                if (Master.formState == EnumHelper.FormState.add)
                {
                    if (Security.GetCurrentUserType() != EnumHelper.UserType.Administrator)State=EnumHelper.UserState.WaitCheck;
                    callBak = bll.Add(State, Password, Name, ShortName, Contacts, Tel, Mobile, Address, Description, Province, City, CityNo, Area, IsLatePay,IsDelivery, DataTypePlus.requestInt(model.PicID));
                }
                else {
                    State = (EnumHelper.UserState)modelUser.State;
                    if(modelUser.State==(int)EnumHelper.UserState.WaitCheck){
                        if(cb_StateCheck.Checked)State=EnumHelper.UserState.Nomal;
                    }else if(cb_IsLock.Checked)State=EnumHelper.UserState.Lock;
                    else State=EnumHelper.UserState.Nomal;
                    callBak = bll.Update(model, State, Password, Name, ShortName, Contacts, Tel, Mobile, Address, Description, Province, City, CityNo, Area, IsLatePay,IsDelivery, DataTypePlus.requestInt(model.PicID));
                }
                    if (!callBak.IsSuccess) { JsHelper.Alert_float(callBak.ErrMsg, false); return; }
                BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Supplier, callBak.ReturnVal, Master.formState == EnumHelper.FormState.add ? EnumHelper.OperateType.Add : EnumHelper.OperateType.Update, "");
                
                //model.Name = Name;
                //model.ShortName = PageValidate.SqlTextClear(tb_ShortName.Text);
                //model.Contacts = PageValidate.SqlTextClear(tb_Contacts.Text);
                //model.Tel = PageValidate.SqlTextClear(tb_Tel.Text);
                //model.Mobile = PageValidate.SqlTextClear(tb_Mobile.Text);
                //model.Address = PageValidate.SqlTextClear(tb_Address.Text);
                //model.Description = PageValidate.SqlTextClear(tb_Description.Text);
                

                //model.Province = PageValidate.SqlTextClear(tb_Province.Text);
                //model.City = PageValidate.SqlTextClear(tb_City.Text);
                //model.Area = PageValidate.SqlTextClear(tb_Area.Text);

                //model.IsLatePay = cb_IsLatePay.Checked;

                //modelUser.Username = Name;
                //modelUser.IsLock = cb_IsLock.Checked;
                //if (Master.formState == EnumHelper.FormState.add)
                //{
                //    //添加User
                //    modelUser.Password = StringPlus.md5(Password);
                //    modelUser.UserType = (int)EnumHelper.UserType.Supplier;
                //    int UserID_new = bllUser.Add(modelUser);
                //    //添加Supplier
                //    model.UserID = UserID_new;
                //    model.Number = "";
                //    int id_new = bll.Add(model);
                //    //更新Supplier.Number
                //    model = bll.GetModel(id_new);
                //    model.Number = "G" + tb_CityNo.Text + "-" + (bll.GetRecordCount(""));
                //    bll.Update(model);
                //}
                //else
                //{
                //    if (Password != "") modelUser.Password = StringPlus.md5(Password);
                //    bllUser.Update(modelUser);
                //    bll.Update(model);
                //}
                Master.SubmitOK();

            }
        }
    }
}